﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;
using CommonService;
using EmailMarketingDomain;
using EmailMarketingService;
using Timer = System.Windows.Forms.Timer;

namespace EmailMarketingSys
{
    public partial class Form1 : Form
    {
        private Timer timerforrefresh;
        private Timer timerforTask;
        private static string LastVerifyCode;
        public Form1()
        {
            InitializeComponent();
            timerforrefresh = new Timer();
            timerforrefresh.Interval = 500;//0.5秒任务
            timerforrefresh.Tick += new EventHandler(timerforrefresh_Tick);
            timerforrefresh.Start();
            timerforTask = new Timer();
            timerforTask.Interval = 2000;//2秒任务
            timerforTask.Tick += new EventHandler(timerforTask_Tick);
            timerforTask.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtTitle.Text=SystemConfigService.GetValue(SystemConfigKey.EmailTitle);
            txtContent.Text = SystemConfigService.GetValue(SystemConfigKey.EmailContent);
            ProxyPostOffice.WorkSpeed = (int)nudProxyPostWorkSpeed.Value * 1000;
            txtViewEmail.Text = "827756467@qq.com";
            Letter letter=new Letter()
            {
                Title = txtTitle.Text,
                Content = txtContent.Text
            };
            NativePostOffice.LetterTemplate = letter;
            NativePostOffice.MaxLetterPoolCount = (int) nudLetterPoolCount.Value;
            NativePostOffice.WorkSpeed = (int) nupNativePostWorkSpeed.Value*1000;
            EmailSendService.AutoUpdateSendAcountHasSendCount();

        }
        private void timerforrefresh_Tick(object sender, EventArgs e)
        {
            lblNativePostWaitedSendCount.Text = PostOfficeMonitor.NativePostOfficeLetterQueueLength.ToString();
            lblNativePostIsWorking.Text = PostOfficeMonitor.NativePostOfficeIsWorking ? "是" : "否";
            btnNativePostStartToWork.Text = PostOfficeMonitor.NativePostOfficeIsWorking ? "停止工作" : "开始工作";
            lblErrorInfo.Text = PostOfficeMonitor.NativePostOfficeStopWorkReason;
            lblALLFinishedCount.Text = PostOfficeMonitor.NativePostOfficeAllFinishedTaskCount.ToString();
            lblAllSuccessFinishedCount.Text = PostOfficeMonitor.NativePostOfficeAllSuccessFinishedTaskCount.ToString();
            lblQQSendCount.Text = PostOfficeMonitor.QQPostOfficeFinishedTaskCount.ToString();
            lblQQSuccessSendCount.Text = PostOfficeMonitor.QQPostOfficeSuccessFinishedTaskCount.ToString();
            lbl126SendCount.Text = PostOfficeMonitor.One26PostOfficeFinishedTaskCount.ToString();
            lbl126SuccessSendCount.Text = PostOfficeMonitor.One26PostOfficeSuccessFinishedTaskCount.ToString();
            lbl163SendCount.Text = PostOfficeMonitor.One63PostOfficeFinishedTaskCount.ToString();
            lbl163SuccessSendCount.Text = PostOfficeMonitor.One63PostOfficeSuccessFinishedTaskCount.ToString();
            NativePostOffice.EnableActivitiesUserEmail = cbEnableActivity.Checked;
            if (PostOfficeMonitor.NativePostOfficeIsWorking)
            {
                btnStart.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            }
            else
            {
                btnStart.BackColor = System.Drawing.Color.Red;
            }
            if (PostOfficeMonitor.NativePostOfficeAllFinishedTaskCount != 0)
            {
                lblAlluccesRate.Text =
              (Math.Round(((double)PostOfficeMonitor.NativePostOfficeAllSuccessFinishedTaskCount / (double)PostOfficeMonitor.NativePostOfficeAllFinishedTaskCount), 2) * 100) + "%";
            }
        }
        private void timerforTask_Tick(object sender, EventArgs e)
        {
            lblQQ24HasSend.Text = SmtpService.GetSmtpServerHasSendCountByName("qq").ToString();
            lbl16324HasSend.Text = SmtpService.GetSmtpServerHasSendCountByName("163").ToString();
            lbl12624HasSend.Text = SmtpService.GetSmtpServerHasSendCountByName("126").ToString();

        }

        private void btnSureContent_Click(object sender, EventArgs e)
        {
            SystemConfigService.SetValue(SystemConfigKey.EmailTitle,txtTitle.Text);
            SystemConfigService.SetValue(SystemConfigKey.EmailContent, txtContent.Text);
            Letter letter = new Letter()
            {
                Title = txtTitle.Text,
                Content = txtContent.Text
            };
            NativePostOffice.LetterTemplate = letter;
            MessageBox.Show("设置成功！");
        }

        private void nudLetterPoolCount_ValueChanged(object sender, EventArgs e)
        {
            NativePostOffice.MaxLetterPoolCount = (int)nudLetterPoolCount.Value;
        }

        private void nupNativePostWorkSpeed_ValueChanged(object sender, EventArgs e)
        {
            NativePostOffice.WorkSpeed = (int)nupNativePostWorkSpeed.Value * 1000;
        }

        private void btnNativePostStartToWork_Click(object sender, EventArgs e)
        {
            if (btnNativePostStartToWork.Text == "开始工作")
            {
                Thread th1 = new Thread(NativePostOffice.GetInstance().StartToWork);
                th1.Start();
            }
            else
            {
                NativePostOffice.GetInstance().StopToWork("手动停止");
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            EmailSendService.StartToSendEmail();
        }

        private void nudProxyPostWorkSpeed_ValueChanged(object sender, EventArgs e)
        {
            ProxyPostOffice.WorkSpeed = (int)nudProxyPostWorkSpeed.Value*1000;
        }

        private void btnInTimeViewEmail_Click(object sender, EventArgs e)
        {
            EmailSendService.AddEmailAddressToWaiteSend(txtViewEmail.Text);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void cbEnableActivity_CheckedChanged(object sender, EventArgs e)
        {
            NativePostOffice.EnableActivitiesUserEmail = cbEnableActivity.Checked;
        }

    }
}
